from scrapy.spider import BaseSpider
from scrapy.selector import Selector

from spider.items import SpiderItem
import MySQLdb


class PriceSpider(BaseSpider):

    name = "page"
    baseurl ="http://www.interface.autohome.com.cn/dealer/specdealerlist/spec/pricelist/"

    conn = MySQLdb.connect(host="localhost",user="spider",passwd="Xjk@4047218?",db="spider")
    cursor = conn.cursor()
    sql = "select autohomepid,url from sp_ah_carmodel order by id asc"
    result = cursor.execute(sql)
    
    carmodel_priceurl = []
    for row in cursor.fetchall():
        pid =  row[1].split("/")[-2]
        url =  baseurl+str(row[0])+"-"+str(pid)+"-1-0-0-0-0-1-1"
        carmodel_priceurl.append(url)

    cursor.close()

    #start_urls = ["http://dealer.autohome.com.cn/china/"]
    #start_urls = ["http://localhost190/a.html"] 
    # start_urls = ["http://www.interface.autohome.com.cn/dealer/specdealerlist/spec/pricelist/18-18103-1-0-0-0-0-1-1","http://www.interface.autohome.com.cn/dealer/specdealerlist/spec/pricelist/923-17375-1-0-0-0-0-1-1"]
    start_urls = carmodel_priceurl


    def parse(self,response):
        
        sel = Selector(response)
        item = SpiderItem()   
        temp = response.url.split("/")[-1].split("-")        
        item["c1"] = temp[0]
        item["c2"] = temp[1]

        area = sel.xpath('//div[@class="page"]')

        item["page"] = area.xpath('.//a[last()-1]/text()').extract()
        item["type"] = "page"

        return item
